package com.mooc.yohoshop.ui.seckill;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.icu.util.TimeUnit;
import android.os.IBinder;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.constant.TimeConstants;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.mooc.yohoshop.R;
import com.mooc.yohoshop.entity.BaseEntity;
import com.mooc.yohoshop.entity.SecKillGoodsEntity;
import com.mooc.yohoshop.ui.base.BaseActivity;
import com.mooc.yohoshop.ui.seckillvalues.SecKillValuesActivity;
import com.mooc.yohoshop.widget.SecKillDivider;

import java.util.HashMap;

public class SecKillActivity extends BaseActivity<SeckillRepo> implements
        View.OnClickListener, OnItemChildClickListener, OnItemClickListener,
        TimeService.UIUpdateInterface {

    private ImageView back;
    private TextView seckill_time;
    private RecyclerView seckill_rv;
    private SecKillAdapter adapter;
    private TimeService timeService;

    @Override
    protected int layout() {
        return R.layout.activity_seckill;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopService(new Intent(this,TimeService.class));
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        bindService(new Intent(this,TimeService.class),connection,BIND_AUTO_CREATE);
    }

    @Override
    protected void onPause() {
        super.onPause();
        unbindService(connection);
    }

    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            TimeService.TimeBinder binder = (TimeService.TimeBinder) service;
            timeService = binder.getService();
            timeService.setUiUpdateInterface(SecKillActivity.this::timeCallBack);
            timeService.time = time;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    @Override
    protected void init() {
        back = f(R.id.back);
        seckill_time = f(R.id.seckill_time);
        seckill_rv = f(R.id.seckill_rv);
        back.setOnClickListener(this::onClick);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        seckill_rv.setLayoutManager(manager);
        seckill_rv.addItemDecoration(new SecKillDivider(this));
        adapter = new SecKillAdapter();
        adapter.setOnItemClickListener(this);
        adapter.setOnItemChildClickListener(this);
        seckill_rv.setAdapter(adapter);
    }

    @Override
    protected void initData() {
        HashMap<String,Object>map = new HashMap<>();
        map.put("page","1");
        request(repo.requestSecKill(map));
    }

    long time = 0;

    @Override
    protected void result(BaseEntity baseEntity) {
        SecKillGoodsEntity entity = (SecKillGoodsEntity) baseEntity;
        adapter.setNewInstance(entity.data);
        Intent intent = new Intent(this,TimeService.class);
        time = entity.data.get(0).nowtime;
        startService(intent);
        bindService(new Intent(this,TimeService.class),connection,BIND_AUTO_CREATE);
    }

    @Override
    public void onClick(View v) {
        finish();
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {

    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        LogUtils.e("onItemClick");
        Intent intent = new Intent(this, SecKillValuesActivity.class);
        intent.putExtra("id",this.adapter.getItem(position).id);
        startActivity(intent);
    }

    @Override
    public void timeCallBack(long time) {
        seckill_time.setText("还有"+TimeUtils.getFitTimeSpan(System.currentTimeMillis(),time*1000, TimeConstants.MIN)+"结束");
    }
}
